package app

import (
	"github.com/gin-gonic/gin"
	"wx_build/core/msg"
	"wx_build/model/tables"
	"wx_build/service"
)

type ProjectApi struct {
}

func (ProjectApi) Add(g *gin.Context) {
	var dataService service.ProjectService
	err := g.ShouldBindJSON(&dataService)
	if err != nil {
		msg.Error(g, err.Error(), err)
		return
	}
	if dataService.ProjectName == "" {
		msg.Error(g, "项目名称不能为空", nil)
		return
	}
	if dataService.GitUrl == "" {
		msg.Error(g, "代码仓库地址不能为空", nil)
		return
	}
	if dataService.Branch == "" {
		msg.Error(g, "代码仓库分支不能为空", nil)
		return
	}
	if dataService.DeploymentWay == "" {
		msg.Error(g, "部署方式不能为空", nil)
		return
	}
	if dataService.Appid == "" {
		msg.Error(g, "小程序id不能为空", nil)
		return
	}
	if dataService.GitUser == "" {
		msg.Error(g, "git用户不能为空", nil)
		return
	}
	if dataService.GitPassword == "" {
		msg.Error(g, "git用户密码不能为空", nil)
		return
	}
	if dataService.CompileCommand == "" {
		msg.Error(g, "编译命令不能为空", nil)
		return
	}
	if dataService.BuildPath == "" {
		msg.Error(g, "编译后路径不能为空", nil)
		return
	}
	dataService.LatestVersion = "1.0.0"
	dataService.Add(g)
	return
}
func (ProjectApi) Update(g *gin.Context) {
	var dataService service.ProjectService
	err := g.ShouldBindJSON(&dataService)
	if err != nil {
		msg.Error(g, err.Error(), err)
		return
	}
	if dataService.ProjectName == "" {
		msg.Error(g, "项目名称不能为空", nil)
		return
	}
	if dataService.GitUrl == "" {
		msg.Error(g, "代码仓库地址不能为空", nil)
		return
	}
	if dataService.Branch == "" {
		msg.Error(g, "代码仓库分支不能为空", nil)
		return
	}
	if dataService.DeploymentWay == "" {
		msg.Error(g, "部署方式不能为空", nil)
		return
	}
	if dataService.Appid == "" {
		msg.Error(g, "小程序id不能为空", nil)
		return
	}
	if dataService.GitUser == "" {
		msg.Error(g, "git用户不能为空", nil)
		return
	}
	if dataService.GitPassword == "" {
		msg.Error(g, "git用户密码不能为空", nil)
		return
	}
	if dataService.CompileCommand == "" {
		msg.Error(g, "编译命令不能为空", nil)
		return
	}
	if dataService.BuildPath == "" {
		msg.Error(g, "编译后路径不能为空", nil)
		return
	}
	if dataService.ProjectId == "" {
		msg.Error(g, "项目id不能为空", nil)
		return
	}
	dataService.Update(g)
	return
}
func (ProjectApi) List(g *gin.Context) {
	var dataService service.ProjectService
	err := g.ShouldBindJSON(&dataService)
	if err != nil {
		msg.Error(g, err.Error(), err)
		return
	}
	if dataService.Page == 0 {
		dataService.Page = 1
	}
	if dataService.Size == 0 {
		dataService.Size = 10
	}
	dataService.List(g)
	return
}
func (ProjectApi) Delete(g *gin.Context) {
	var dataService service.ProjectService
	err := g.ShouldBindJSON(&dataService)
	if err != nil {
		msg.Error(g, err.Error(), err)
		return
	}
	if dataService.ProjectId == "" {
		msg.Error(g, "项目id不能为空", err)
		return
	}
	dataService.Delete(g)
	return
}
func (ProjectApi) GetBranchList(g *gin.Context) {
	var dataService service.ProjectService
	err := g.ShouldBindJSON(&dataService)
	if err != nil {
		msg.Error(g, err.Error(), err)
		return
	}
	if dataService.GitUrl == "" {
		msg.Error(g, "git仓库地址不能为空", nil)
		return
	}
	if dataService.GitUser == "" {
		msg.Error(g, "git用户名不能为空", nil)
		return
	}
	if dataService.GitPassword == "" {
		msg.Error(g, "git密码不能为空", nil)
		return
	}
	dataService.GetBranchList(g)
	return
}
func (ProjectApi) ProjectBuild(g *gin.Context) {
	var dataService service.ProjectService
	err := g.ShouldBindJSON(&dataService)
	if err != nil {
		msg.Error(g, err.Error(), err)
		return
	}
	if dataService.ProjectId == "" {
		msg.Error(g, "项目id不能为空", nil)
		return
	}
	dataService.ProjectBuild(g)
	return
}
func (ProjectApi) AutoBuild(g *gin.Context) {
	projectId := g.Param("project_id")
	var dataService service.ProjectService
	dataService.ProjectId = projectId
	project := tables.Project{ProjectId: projectId}
	err := project.QueryOne(&project)
	if err != nil {
		msg.Error(g, "项目不存在", err)
		return
	}
	dataService.ProjectBuild(g)
	return
}
